package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Comment;
import com.khshop.pojo.Order;
import com.khshop.service.CommentService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-15
 */
@Api(tags = "评论 前端控制器")
@Controller
@RequestMapping("/")
public class CommentController {

    @Autowired
    CommentService commentService;

    //前端发表评价【要求订单状态为待评价才可以发表评价信息，并考虑过滤敏感数据】
    @ApiOperation("前端发表评价")
    @PostMapping("/addComment")
    @ResponseBody
    public AjaxResult addComment(Comment comment){

        if (commentService.addComment(comment).isFlag()) {
            return  AjaxResult.success("发表成功");
        } else {
            return  AjaxResult.error("发表失败，请稍后重试！");
        }
    }

    //后端展示评价数据列表
    @ApiOperation("后端展示评价数据列表")
    @PostMapping("/showComment")
    @ResponseBody
    public AjaxResult showComment(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "limit",defaultValue = "15") Integer limit){

        ServiceResult<IPage<Comment>> serviceResult = commentService.showComment(page, limit);
        if (serviceResult.isFlag()) {
            return  AjaxResult.success("修改成功", (int) serviceResult.getData().getTotal(),serviceResult.getData());
        } else {
            return  AjaxResult.error("修改失败，请稍后重试！");
        }
    }

    //后端删除评价数据
    @ApiOperation("后端删除评价数据")
    @PostMapping("/delComment")
    @ResponseBody
    public AjaxResult delComment(Long []id){

        if (commentService.delComment(id).isFlag()) {
            return  AjaxResult.success();
        } else {
            return  AjaxResult.error();
        }
    }

    //回复评价【考虑一条评价可以追加回复内容】
    @ApiOperation("回复评价")
    @PostMapping("/replyComment")
    @ResponseBody
    public AjaxResult replyComment(Long id,String newComment){

        if (commentService.replyComment(id,newComment).isFlag()) {
            return  AjaxResult.success();
        } else {
            return  AjaxResult.error();
        }
    }

    //根据条件查询评价数据
    @ApiOperation("根据条件查询评价数据")
    @PostMapping("/searchComment")
    @ResponseBody
    public AjaxResult searchComment(@RequestBody Map<String,Object> map,
                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                    @RequestParam(value = "limit",defaultValue = "15") Integer limit){

        ServiceResult<IPage<Comment>> serviceResult = commentService.searchComment(map,page, limit);
        if (serviceResult.isFlag()) {
            return  AjaxResult.success("修改成功", (int) serviceResult.getData().getTotal(),serviceResult.getData());
        } else {
            return  AjaxResult.error("修改失败，请稍后重试！");
        }
    }

    //审核
    @ApiOperation("审核")
    @PostMapping("/checkComment")
    @ResponseBody
    public AjaxResult checkComment(Long id){

        if (commentService.checkComment(id).isFlag()) {
            return  AjaxResult.success();
        } else {
            return  AjaxResult.error();
        }
    }

}

